<?php

namespace app\award;

use app\models\NewLevel;
use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\OrderActive;
use app\models\product\Product;
use app\models\product\ProductArea;
use app\models\Queue;
use app\models\SetProductCommission;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserAddress;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;
use app\util\Util;
use yii\helpers\ArrayHelper;

class Award
{
    protected $order;
    protected $user;

    protected $set;

    protected $total_pay;

    protected $amount;
    protected $quantity;

    // 是否只处理分红, 这个改成周返了 [日返]

    public function __construct(Order $order, $is_reset = false, $is_div = false)
    {
        if (!$is_div) {
            $has = UserCommission::find()
                ->where(['order_id' => $order->id])
                ->one();

            if ($has) {
                return;
            }
        }

        $this->order = $order;
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $this->total_pay = $this->order->total_pay - $this->order->total_freight;

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        $this->set = SetUserUp::getLastOne();

        //  总金额
        $this->amount = $this->order->total_pay - $this->order->total_freight;

        // 总数量
        $items = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->all();
        $quantity = 0;
        foreach ($items as $item) {
            $quantity += $item['quantity'];
        }
        $this->quantity = $quantity;

        // 测试下单速度
        // var_dump(444);exit;

        $is_bao = ProductArea::checkBao($product_id);
        $is_repeat = ProductArea::checkRepeat($product_id);

        // var_dump($is_bao, $is_repeat);exit;

        if ($is_bao) {
            // $this->_bao();

            // 分红使用
            $this->order->is_repeat = 2;
            $this->order->save();

            // $this->bao();

            // var_dump($is_div);exit;

            if ($is_div) {
                if ($is_reset) {
                    $this->bao();
                }
                $this->baoDiv();
                $this->runDiv();
            } else {
                $this->bao();
            }
        }

        // var_dump(444);exit;

        if ($is_repeat) {
            $this->order->is_repeat = 1;
            $this->order->save();

            // $this->_diff();


            if ($is_div) {
                $this->repeatDiv();
                $this->runDiv();
            } else {
                if ($is_reset) {
                    $this->repeat();
                }
                $this->repeat();
            }
        }

        // var_dump(444);exit;

    }

    public function repeat()
    {
        $type = 21;
        $name = '销售奖';

        $chain = Chain::getReferrerChain($this->user->id, [
            // UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4'],
            UserLevel::$keys['vip5'],
            UserLevel::$keys['vip6'],
        ], true);

        // var_dump(ArrayHelper::getColumn($chain, 'id'));exit;

        // var_dump(444);exit;

        // $has_vip1 = false;
        $has_vip2 = false;
        $has_vip3 = false;
        $has_vip4 = false;
        $has_vip5 = false;
        $has_vip6 = false;

        // $diff_vip1 = 0;
        $diff_vip2 = 0;
        $diff_vip3 = 0;
        $diff_vip4 = 0;
        $diff_vip5 = 0;
        $diff_vip6 = 0;

        // 本人的折扣比例
        $my = 0;
        if ($this->user->level == UserLevel::$keys['vip2']) {
            $diff_vip2 = $this->set->re_r_vip2 / 100 * $this->amount;
        }
        if ($this->user->level == UserLevel::$keys['vip3']) {
            $diff_vip2 = $this->set->re_r_vip3 / 100 * $this->amount;
        }
        if ($this->user->level == UserLevel::$keys['vip4']) {
            $diff_vip2 = $this->set->re_r_vip4 / 100 * $this->amount;
        }
        if ($this->user->level == UserLevel::$keys['vip5']) {
            $diff_vip2 = $this->set->re_r_vip5 / 100 * $this->amount;
        }
        if ($this->user->level == UserLevel::$keys['vip6']) {
            $diff_vip2 = $this->set->re_r_vip6 / 100 * $this->amount;
        }

        foreach ($chain as $user) {
            if ($user->level <= $this->user->level) {
                continue;
            }

            if ((!$has_vip2 && !$has_vip3 && !$has_vip4 && !$has_vip5 && !$has_vip6) && $user->level == UserLevel::$keys['vip2']) {
                $diff_vip2 = $this->set->re_r_vip2 / 100 * $this->amount;
                $this->saveCommission($user->id, $diff_vip2 - $my, $type, $name);
                $has_vip2 = true;
                $this->_m($user, $diff_vip2 - $my);
            }
            if ((!$has_vip3 && !$has_vip4 && !$has_vip5 && !$has_vip6) && $user->level == UserLevel::$keys['vip3']) {
                $diff_vip3 = $this->set->re_r_vip3 / 100 * $this->amount - $diff_vip2;
                $this->saveCommission($user->id, $diff_vip3 - $my, $type, $name);
                $has_vip3 = true;
                $this->_m($user, $diff_vip3 - $my);
            }
            if ((!$has_vip4 && !$has_vip5 && !$has_vip6) && $user->level == UserLevel::$keys['vip4']) {
                $diff_vip4 = $this->set->re_r_vip4 / 100 * $this->amount - $diff_vip3 - $diff_vip2;
                $this->saveCommission($user->id, $diff_vip4 - $my, $type, $name);
                $has_vip4 = true;
                $this->_m($user, $diff_vip4 - $my);
            }
            if ((!$has_vip5 && !$has_vip6) && $user->level == UserLevel::$keys['vip5']) {
                $diff_vip5 = $this->set->re_r_vip5 / 100 * $this->amount - $diff_vip4 - $diff_vip3 - $diff_vip2;
                $this->saveCommission($user->id, $diff_vip5 - $my, $type, $name);
                $has_vip5 = true;
                $this->_m($user, $diff_vip5 - $my);
            }
            if ((!$has_vip6) && $user->level == UserLevel::$keys['vip6']) {
                $diff_vip6 = $this->set->re_r_vip6 / 100 * $this->amount - $diff_vip5 - $diff_vip4 - $diff_vip3 - $diff_vip2;
                $this->saveCommission($user->id, $diff_vip6 - $my, $type, $name);
                $has_vip6 = true;
                $this->_m($user, $diff_vip6 - $my);
                break;
            }
        }

        // var_dump(444);exit;

        $type = 23;
        $name = '平级奖';
        $r = Chain::getReferrer($this->user->id, true);
        if ($r && $r->level == $this->user->level) {
            $commission = 0;
            // if ($r->level == UserLevel::$keys['vip1']) {
            //     $commission = $this->set->r_vip1;
            // }
            if ($r->level == UserLevel::$keys['vip2']) {
                $commission = $this->set->re_e_vip2;
            }
            if ($r->level == UserLevel::$keys['vip3']) {
                $commission = $this->set->re_e_vip3;
            }
            if ($r->level == UserLevel::$keys['vip4']) {
                $commission = $this->set->re_e_vip4;
            }
            if ($r->level == UserLevel::$keys['vip5']) {
                $commission = $this->set->re_e_vip5;
            }
            if ($r->level == UserLevel::$keys['vip6']) {
                $commission = $this->set->re_e_vip6;
            }
            $this->saveCommission($r->id, $commission / 100 * $this->amount, $type, $name);
        }

        // var_dump(444);exit;

        $type = 24;
        $name = '地区包裹奖';

        $a1 = $this->set->re_b_vip1 * $this->quantity;
        $a2 = $this->set->re_b_vip2 * $this->quantity;
        $a3 = $this->set->re_b_vip3 * $this->quantity;

        $has1 = false;
        $has2 = false;
        $has3 = false;

        $address = UserAddress::findOne(['id' => $this->order->user_address]);
        if ($address) {
            if ($address->area_id) {
                $area = User::findOne(['address_up' => $address->area_id]);
                if ($area) {
                    $this->saveCommission($area->id, $a1, $type, $name);
                    $has1 = true;
                    // $this->_b($area->id);
                }
            }
            if ($address->city_id) {
                $city = User::findOne(['address_up' => $address->city_id]);
                if ($city) {
                    if ($has1) {
                        $a2 -= $a1;
                    }
                    $this->saveCommission($city->id, $a2, $type, $name);
                    $has2 = true;
                    // $this->_b($city->id);
                }
            }
            if ($address->province_id) {
                $province = User::findOne(['address_up' => $address->province_id]);
                if ($province) {
                    if ($has1) {
                        $a3 -= $a1;
                    }
                    if ($has2) {
                        $a3 -= $a2;
                    }
                    $this->saveCommission($province->id, $a3, $type, $name);
                    // $this->_b($province->id);
                }
            }
        }

        // var_dump(444);exit;


    }

    // 复购分红
    public function repeatDiv()
    {
        $type = 25;
        $name = '分红';
        $amount = $this->amount * 0.05;

        if ($this->set->re_p_vip1 > 0 && true) {
            $vip1 = User::find()->where(['level' => UserLevel::$keys['vip1']])->all();
            // var_dump(444, $vip1);exit;
            if (!empty($vip1)) {
                $c1 = $this->set->re_p_vip1 / 100 * $amount / count($vip1);
                // 处理不能分的
                $data = [];
                foreach ($vip1 as $user) {
                    if (!$this->_isBei($user, $c1)) {
                        array_push($data, $user->id);
                    }
                }
                if (!empty($data)) {
                    $c1 = $this->set->re_p_vip1 / 100 * $amount / count($data);
                    foreach ($data as $user_id) {
                        $this->saveCommission($user_id, $c1, $type, $name. '1');
                    }
                }
            }
        }

        // var_dump(444);exit;
        if ($this->set->re_p_vip2 > 0 && true) {
            $vip2 = User::find()->where(['level' => UserLevel::$keys['vip2']])->all();
            if (!empty($vip2)) {
                $c2 = $this->set->re_p_vip2 / 100 * $amount / count($vip2);
                // 处理不能分的
                $data = [];
                foreach ($vip2 as $user) {
                    if (!$this->_isBei($user, $c2)) {
                        array_push($data, $user->id);
                    }
                }
                if (!empty($data)) {
                    $c2 = $this->set->re_p_vip2 / 100 * $amount / count($data);
                    foreach ($data as $user_id) {
                        $this->saveCommission($user_id, $c2, $type, $name . '2');
                    }
                }
            }
        }
        if ($this->set->re_p_vip3 > 0 && true) {
            $vip3 = User::find()->where(['level' => UserLevel::$keys['vip3']])->all();
            if (!empty($vip3)) {
                $c3 = $this->set->re_p_vip3 / 100 * $amount / count($vip3);
                // 处理不能分的
                $data = [];
                foreach ($vip3 as $user) {
                    if (!$this->_isBei($user, $c3)) {
                        array_push($data, $user->id);
                    }
                }
                if (!empty($data)) {
                    $c3 = $this->set->re_p_vip3 / 100 * $amount / count($data);
                    foreach ($data as $user_id) {
                        $this->saveCommission($user_id, $c3, $type, $name . '3');
                    }
                }
            }
        }
        if ($this->set->re_p_vip4 > 0 && true) {
            $vip4 = User::find()->where(['level' => UserLevel::$keys['vip4']])->all();
            if (!empty($vip4)) {
                $c4 = $this->set->re_p_vip4 / 100 * $amount / count($vip4);
                // 处理不能分的
                $data = [];
                foreach ($vip4 as $user) {
                    if (!$this->_isBei($user, $c4)) {
                        array_push($data, $user->id);
                    }
                }
                if (!empty($data)) {
                    $c4 = $this->set->re_p_vip4 / 100 * $amount / count($data);
                    foreach ($data as $user_id) {
                        $this->saveCommission($user_id, $c4, $type, $name . '4');
                    }
                }
            }
        }
        if ($this->set->re_p_vip5 > 0 && true) {
            $vip5 = User::find()->where(['level' => UserLevel::$keys['vip5']])->all();
            if (!empty($vip5)) {
                $c5 = $this->set->re_p_vip5 / 100 * $amount / count($vip5);
                // 处理不能分的
                $data = [];
                foreach ($vip5 as $user) {
                    if (!$this->_isBei($user, $c5)) {
                        array_push($data, $user->id);
                    }
                }
                if (!empty($data)) {
                    $c5 = $this->set->re_p_vip5 / 100 * $amount / count($data);
                    foreach ($data as $user_id) {
                        $this->saveCommission($user_id, $c5, $type, $name . '5');
                    }
                }
            }
        }
        if ($this->set->re_p_vip6 > 0 && true) {
            $vip6 = User::find()->where(['level' => UserLevel::$keys['vip6']])->all();
            if (!empty($vip6)) {
                $c6 = $this->set->re_p_vip6 / 100 * $amount / count($vip6);
                // 处理不能分的
                $data = [];
                foreach ($vip6 as $user) {
                    if (!$this->_isBei($user, $c6)) {
                        array_push($data, $user->id);
                    }
                }
                if (!empty($data)) {
                    $c6 = $this->set->re_p_vip6 / 100 * $amount / count($data);
                    foreach ($data as $user_id) {
                        $this->saveCommission($user_id, $c6, $type, $name . '6');
                    }
                }
            }
        }
    }

    // 复购服务奖
    public function _m($user, $amount)
    {
        $type = 22;
        $name = '服务奖';

        $r = Chain::getReferrer($user->id, true);
        if ($r && $r->level >= $user->level) {
            $commission = 0;
            // if ($r->level == UserLevel::$keys['vip1']) {
            //     $commission = $this->set->r_vip1;
            // }
            if ($r->level == UserLevel::$keys['vip2']) {
                $commission = $this->set->re_m_vip2;
            }
            if ($r->level == UserLevel::$keys['vip3']) {
                $commission = $this->set->re_m_vip3;
            }
            if ($r->level == UserLevel::$keys['vip4']) {
                $commission = $this->set->re_m_vip4;
            }
            if ($r->level == UserLevel::$keys['vip5']) {
                $commission = $this->set->re_m_vip5;
            }
            if ($r->level == UserLevel::$keys['vip6']) {
                $commission = $this->set->re_m_vip6;
            }
            $this->saveCommission($r->id, $commission / 100 * $amount, $type, $name);
        }

    }

    public function bao()
    {
        $type = 1;
        $name = '销售奖';

        $r = Chain::getReferrer($this->user->id, true);
        if ($r) {
            $commission = 0;
            if ($r->level == UserLevel::$keys['vip1']) {
                $commission = $this->set->r_vip1;
            }
            if ($r->level == UserLevel::$keys['vip2']) {
                $commission = $this->set->r_vip2;
            }
            if ($r->level == UserLevel::$keys['vip3']) {
                $commission = $this->set->r_vip3;
            }
            if ($r->level == UserLevel::$keys['vip4']) {
                $commission = $this->set->r_vip4;
            }
            if ($r->level == UserLevel::$keys['vip5']) {
                $commission = $this->set->r_vip5;
            }
            if ($r->level == UserLevel::$keys['vip6']) {
                $commission = $this->set->r_vip6;
            }
            $this->saveCommission($r->id, $commission * $this->quantity, $type, $name);
        }

        $type = 2;
        $name = '管理奖';
        $chain = Chain::getReferrerChain($this->user->id, [
            // UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4'],
            UserLevel::$keys['vip5'],
            UserLevel::$keys['vip6'],
        ], true);

        // $has_vip1 = false;
        $has_vip2 = false;
        $has_vip3 = false;
        $has_vip4 = false;
        $has_vip5 = false;
        $has_vip6 = false;

        // $diff_vip1 = 0;
        $diff_vip2 = 0;
        $diff_vip3 = 0;
        $diff_vip4 = 0;
        $diff_vip5 = 0;
        $diff_vip6 = 0;

        foreach ($chain as $user) {
            if ((!$has_vip2 && !$has_vip3 && !$has_vip4 && !$has_vip5 && !$has_vip6) && $user->level == UserLevel::$keys['vip2']) {
                $diff_vip2 = $this->set->m_vip2;
                $this->saveCommission($user->id, $diff_vip2 * $this->quantity, $type, $name);
                $has_vip2 = true;
            }
            if ((!$has_vip3 && !$has_vip4 && !$has_vip5 && !$has_vip6) && $user->level == UserLevel::$keys['vip3']) {
                $diff_vip3 = $this->set->m_vip3 - $diff_vip2;
                $this->saveCommission($user->id, $diff_vip3 * $this->quantity, $type, $name);
                $has_vip3 = true;
            }
            if ((!$has_vip4 && !$has_vip5 && !$has_vip6) && $user->level == UserLevel::$keys['vip4']) {
                $diff_vip4 = $this->set->m_vip4 - $diff_vip3 - $diff_vip2;
                $this->saveCommission($user->id, $diff_vip4 * $this->quantity, $type, $name);
                $has_vip4 = true;
            }
            if ((!$has_vip5 && !$has_vip6) && $user->level == UserLevel::$keys['vip5']) {
                $diff_vip5 = $this->set->m_vip5 - $diff_vip4 - $diff_vip3 - $diff_vip2;
                $this->saveCommission($user->id, $diff_vip5 * $this->quantity, $type, $name);
                $has_vip5 = true;
            }
            if ((!$has_vip6) && $user->level == UserLevel::$keys['vip6']) {
                $diff_vip6 = $this->set->m_vip6 - $diff_vip5 - $diff_vip4 - $diff_vip3 - $diff_vip2;
                $this->saveCommission($user->id, $diff_vip6 * $this->quantity, $type, $name);
                $has_vip6 = true;
                break;
            }
        }

        $type = 3;
        $name = '地区包裹奖';

        $a1 = $this->set->a_vip1 * $this->quantity;
        $a2 = $this->set->a_vip2 * $this->quantity;
        $a3 = $this->set->a_vip3 * $this->quantity;

        $has1 = false;
        $has2 = false;
        $has3 = false;

        $address = UserAddress::findOne(['id' => $this->order->user_address]);
        if ($address) {
            if ($address->area_id) {
                $area = User::findOne(['address_up' => $address->area_id]);
                if ($area) {
                    $this->saveCommission($area->id, $a1, $type, $name);
                    $has1 = true;
                    $this->_b($area->id);
                }
            }
            if ($address->city_id) {
                $city = User::findOne(['address_up' => $address->city_id]);
                if ($city) {
                    if ($has1) {
                        $a2 -= $a1;
                    }
                    $this->saveCommission($city->id, $a2, $type, $name);
                    $has2 = true;
                    $this->_b($city->id);
                }
            }
            if ($address->province_id) {
                $province = User::findOne(['address_up' => $address->province_id]);
                if ($province) {
                    if ($has1) {
                        $a3 -= $a1;
                    }
                    if ($has2) {
                        $a3 -= $a2;
                    }
                    $this->saveCommission($province->id, $a3, $type, $name);
                    $this->_b($province->id);
                }
            }
        }



    }

    // 报单分红奖
    public function baoDiv()
    {
        $type = 5;
        $name = '分红';
        $amount = $this->amount * 0.05;

        // var_dump(444);exit;

        if ($this->set->p_vip1 > 0 && true) {
            // $vip1 = User::find()->where(['level' => UserLevel::$keys['vip1']])->all();
            $vip1 = User::find()->where(['level' => UserLevel::$keys['vip1']])->select(['id', 'level'])->all();

            if (!empty($vip1)) {
                $c1 = $this->set->p_vip1 / 100 * $amount / count($vip1);
                // 处理不能分的
                $data = [];
                foreach ($vip1 as $user) {
                    if (!$this->_isBei($user, $c1)) {
                        array_push($data, $user->id);
                    }
                }
                if (!empty($data)) {
                    $c1 = $this->set->p_vip1 / 100 * $amount / count($data);
                    foreach ($data as $user_id) {
                        $this->saveCommission($user_id, $c1, $type, $name. '1');
                    }
                }
            }
        }
        // var_dump(444);exit;
        if ($this->set->p_vip2 > 0 && true) {
            $vip2 = User::find()->where(['level' => UserLevel::$keys['vip2']])->select(['id', 'level'])->all();
            if (!empty($vip2)) {
                $c2 = $this->set->p_vip2 / 100 * $amount / count($vip2);
                // 处理不能分的
                $data = [];
                foreach ($vip2 as $user) {
                    if (!$this->_isBei($user, $c2)) {
                        array_push($data, $user->id);
                    }
                }
                if (!empty($data)) {
                    $c2 = $this->set->p_vip2 / 100 * $amount / count($data);
                    foreach ($data as $user_id) {
                        $this->saveCommission($user_id, $c2, $type, $name . '2');
                    }
                }
            }
        }
        // var_dump(444);exit;
        if ($this->set->p_vip3 > 0 && true) {
            $vip3 = User::find()->where(['level' => UserLevel::$keys['vip3']])->select(['id', 'level'])->all();
            if (!empty($vip3)) {
                $c3 = $this->set->p_vip3 / 100 * $amount / count($vip3);
                // 处理不能分的
                $data = [];
                foreach ($vip3 as $user) {
                    if (!$this->_isBei($user, $c3)) {
                        array_push($data, $user->id);
                    }
                }
                if (!empty($data)) {
                    $c3 = $this->set->p_vip3 / 100 * $amount / count($data);
                    foreach ($data as $user_id) {
                        $this->saveCommission($user_id, $c3, $type, $name . '3');
                    }
                }
            }
        }
        if ($this->set->p_vip4 > 0 && true) {
            $vip4 = User::find()->where(['level' => UserLevel::$keys['vip4']])->select(['id', 'level'])->all();
            if (!empty($vip4)) {
                $c4 = $this->set->p_vip4 / 100 * $amount / count($vip4);
                // 处理不能分的
                $data = [];
                foreach ($vip4 as $user) {
                    if (!$this->_isBei($user, $c4)) {
                        array_push($data, $user->id);
                    }
                }
                if (!empty($data)) {
                    $c4 = $this->set->p_vip4 / 100 * $amount / count($data);
                    foreach ($data as $user_id) {
                        $this->saveCommission($user_id, $c4, $type, $name . '4');
                    }
                }
            }
        }
        if ($this->set->p_vip5 > 0 && true) {
            $vip5 = User::find()->where(['level' => UserLevel::$keys['vip5']])->select(['id', 'level'])->all();
            if (!empty($vip5)) {
                $c5 = $this->set->p_vip5 / 100 * $amount / count($vip5);
                // 处理不能分的
                $data = [];
                foreach ($vip5 as $user) {
                    if (!$this->_isBei($user, $c5)) {
                        array_push($data, $user->id);
                    }
                }
                if (!empty($data)) {
                    $c5 = $this->set->p_vip5 / 100 * $amount / count($data);
                    foreach ($data as $user_id) {
                        $this->saveCommission($user_id, $c5, $type, $name . '5');
                    }
                }
            }
        }
        if ($this->set->p_vip6 > 0 && true) {
            $vip6 = User::find()->where(['level' => UserLevel::$keys['vip6']])->select(['id', 'level'])->all();
            if (!empty($vip6)) {
                $c6 = $this->set->p_vip6 / 100 * $amount / count($vip6);
                // 处理不能分的
                $data = [];
                foreach ($vip6 as $user) {
                    if (!$this->_isBei($user, $c6)) {
                        array_push($data, $user->id);
                    }
                }
                if (!empty($data)) {
                    $c6 = $this->set->p_vip6 / 100 * $amount / count($data);
                    foreach ($data as $user_id) {
                        $this->saveCommission($user_id, $c6, $type, $name . '6');
                    }
                }
            }
        }


    }

    // 主要处理分红待返, 变成已返
    public function runDiv()
    {
        $commissions = UserCommission::find()
            ->where(['order_id' => $this->order->id])
            ->andWhere(['status' => 0])
            // 只处理分红
            ->andWhere(['award_type' => [5, 25]])
            ->all();
        foreach ($commissions as $c) {
            $c->status = 1;
            $c->save();

            $account = UserAccount::findOne(['user_id' => $c->user_id]);
            if ($account) {
                if ($account->await_balance >= $c->balance) {
                    $account->await_balance -= $c->balance;
                    $account->done_balance += $c->balance;
                    $account->balance += $c->balance;
                    $account->save();
                }
            }
        }
    }

    // 推荐包裹奖
    public function _b($user_id)
    {
        $type = 4;
        $name = '推荐包裹奖';

        $r = Chain::getReferrer($user_id, true);
        if ($r) {
            $commission = 0;
            if ($r->level == UserLevel::$keys['vip1']) {
                $commission = $this->set->b_vip1;
            }
            if ($r->level == UserLevel::$keys['vip2']) {
                $commission = $this->set->b_vip2;
            }
            if ($r->level == UserLevel::$keys['vip3']) {
                $commission = $this->set->b_vip3;
            }
            if ($r->level == UserLevel::$keys['vip4']) {
                $commission = $this->set->b_vip4;
            }
            if ($r->level == UserLevel::$keys['vip5']) {
                $commission = $this->set->b_vip5;
            }
            if ($r->level == UserLevel::$keys['vip6']) {
                $commission = $this->set->b_vip6;
            }
            $this->saveCommission($r->id, $commission * $this->quantity, $type, $name);
        }
    }

    // 判断佣金是否满位数
    public function _isBei($user, $amount)
    {
        // 优化数据库查询性能
        // 使用 Yii 的查询构建器进行优化
        $order = \Yii::$app->db->createCommand(
            "SELECT SUM(total_price) FROM `order` WHERE user_id=:user_id AND is_repeat=2",
            [':user_id' => $user->id]
        )->queryScalar();
        
        $commission = \Yii::$app->db->createCommand(
            "SELECT SUM(balance) FROM `user_commission` WHERE user_id=:user_id AND award_type=5",
            [':user_id' => $user->id]
        )->queryScalar();
        
        // 使用更简洁的方式设置倍数
        $levelMap = [
            UserLevel::$keys['vip1'] => 'px_vip1',
            UserLevel::$keys['vip2'] => 'px_vip2',
            UserLevel::$keys['vip3'] => 'px_vip3',
            UserLevel::$keys['vip4'] => 'px_vip4',
            UserLevel::$keys['vip5'] => 'px_vip5',
            UserLevel::$keys['vip6'] => 'px_vip6'
        ];
        
        $bei = 1;
        if (isset($levelMap[$user->level]) && isset($this->set->{$levelMap[$user->level]})) {
            $bei = $this->set->{$levelMap[$user->level]};
        }

        return ($commission ?: 0) + $amount > ($order ?: 0) * $bei;
    }

    // 复购, 判断佣金是否满位数
    public function _isBei2($user, $amount)
    {
        // 订单金额, 红分佣金总计, 现在分红,
        // 是否超过
        $order = Order::find()
            ->where(['user_id' => $user->id])
            ->andWhere(['is_repeat' => 1])
            ->sum('total_price');
        $commission = UserCommission::find()
            ->where(['user_id' => $user->id])
            ->andWhere(['award_type' => 25])
            ->sum('balance');
        $bei = 1;
        if ($user->level == UserLevel::$keys['vip1']) {
            $bei = $this->set->re_p_vip1;
        }
        if ($user->level == UserLevel::$keys['vip2']) {
            $bei = $this->set->re_p_vip2;
        }
        if ($user->level == UserLevel::$keys['vip3']) {
            $bei = $this->set->re_p_vip3;
        }
        if ($user->level == UserLevel::$keys['vip4']) {
            $bei = $this->set->re_p_vip4;
        }
        if ($user->level == UserLevel::$keys['vip5']) {
            $bei = $this->set->re_p_vip5;
        }
        if ($user->level == UserLevel::$keys['vip6']) {
            $bei = $this->set->re_p_vip6;
        }

        return $commission + $amount > $order * $bei;
    }


    // private function _diff()
    // {
    //     $name = '差价';
    //     $type = 51;
    //
    //     $has_vip2 = false;
    //
    //     $diff_vip2 = 0;
    //
    //     $chain = Chain::getReferrerChain($this->order->user_id, [
    //         UserLevel::$keys['vip2'],
    //     ], true);
    //
    //     foreach ($chain as $user) {
    //         if ((!$has_vip2) && $user->level == UserLevel::$keys['vip2']) {
    //             $diff_vip2 = $this->_diffKey('vip2');
    //             $this->saveCommission($user->id, $diff_vip2, $type, $name);
    //             $has_vip2 = true;
    //             break;
    //         }
    //     }
    // }
    //
    // private function _diffKey($diff_key)
    // {
    //     $order_items = OrderItem::find()
    //         ->where(['order_id' => $this->order->id])
    //         ->all();
    //
    //     $sum = 0;
    //
    //     foreach ($order_items as $item) {
    //         $product = SetProductCommission::findOne(['product_id' => $item->product_id])->toArray();
    //         $sum += ($item['price'] - $product[$diff_key]) * $item['quantity'];
    //     }
    //
    //     return $sum;
    // }
    //
    //
    // public function _bao()
    // {
    //     $award_name1 = '直推';
    //     $award_type1 = 11;
    //
    //     $award_name2 = '间推';
    //     $award_type2 = 12;
    //
    //     $quantity = $this->order->total_quantity;
    //
    //
    //     $r1 = Chain::getReferrer($this->user->id, true);
    //     if ($r1) {
    //         if ($r1->level == UserLevel::$keys['vip2']) {
    //             $this->saveCommission($r1->id, $this->set->r2 * $quantity, $award_type1, $award_name1);
    //             // 平级
    //             $this->_equal($r1, $this->set->r2 * $quantity);
    //         } else if ($r1->level == UserLevel::$keys['vip1']) {
    //             $this->saveCommission($r1->id, $this->set->r1, $award_type1, $award_name1);
    //
    //             // 间推
    //             $r2 = $this->getIndirect($r1->id);
    //             if ($r2) {
    //
    //                 $commission = ($this->set->r2 - $this->set->r1) * $quantity;
    //
    //                 $this->saveCommission($r2->id, $commission, $award_type2, $award_name2);
    //                 // 平级
    //                 $this->_equal($r2, $commission);
    //             }
    //         }
    //
    //     }
    // }
    //
    // // 最终的平级奖
    // public function _equal(User $user, $amount)
    // {
    //     $award_name = '平级奖';
    //     $award_type = 21;
    //
    //     $chain = Chain::getReferrerChain($user->id, [
    //         UserLevel::$keys['vip2'],
    //     ], true);
    //
    //     $commission = $this->set->e / 100 * $amount;
    //
    //     if ($chain) {
    //         $r = $chain[0];
    //         $this->saveCommission($r->id, $commission, $award_type, $award_name);
    //     }
    //
    // }


    // 保存佣金
    protected function saveCommission($user_id, $balance, $type, $name)
    {
        if ($balance >= 0.01) {
            $model = new UserCommission();
            $model->user_id = $user_id;
            $model->fans_id = $this->user->id;
            $model->order_id = $this->order->id;
            $model->balance = $balance;
            $model->status = 0;
            $model->award_name = $name;
            $model->award_type = $type;
            $model->save();

            $account = UserAccount::findOne(['user_id' => $user_id]);
            if ($account) {
                // $account->balance += $balance;
                $account->await_balance += $balance;
                $account->save();
            }
        }
    }

    // 2+1 间推
    // private function getIndirect($user_id)
    // {
    //     $the_referrer = Chain::getReferrer($user_id, true);
    //     if (!$the_referrer) {
    //         return null;
    //     }
    //
    //     // 处理两线条
    //     if ($the_referrer->level >= UserLevel::$keys['vip2']) {
    //
    //         // 判断是否属于前2个直推
    //         $children = Chain::getChildrenSort($the_referrer->id, ['id']);
    //         // var_dump($the_referrer->id, $children);exit;
    //
    //         // if (count($children) > $this->set->vip2) {
    //         if (count($children) >= 2) {
    //             $children = [$children[0], $children[1]];
    //             $in = in_array($user_id, $children);
    //             if (!$in) {
    //                 return $the_referrer;
    //             } else {
    //                 return $this->getIndirect($the_referrer->id);
    //             }
    //         } else {
    //             return $this->getIndirect($the_referrer->id);
    //         }
    //     }
    //
    //     return $this->getIndirect($the_referrer->id);
    // }
}